package com.learner.ucenter.controller.api;


import com.alibaba.fastjson.JSON;
import com.learner.common.exception.ExceptionUtil;
import com.learner.common.exception.RestException;
import com.learner.common.util.JwtInfo;
import com.learner.common.util.JwtUtils;
import com.learner.pojo.trade.dto.MemberDto;
import com.learner.pojo.user.User;
import com.learner.pojo.user.vo.DelFenZuRequestVo;
import com.learner.pojo.user.vo.EditFenZuRequestVo;
import com.learner.pojo.user.vo.LoginVo;
import com.learner.pojo.user.vo.ModifyFriendBeiZhuRequestVo;
import com.learner.pojo.user.vo.ModifyFriendFenZuRequestVo;
import com.learner.pojo.user.vo.NewFenZuRequestVo;
import com.learner.pojo.user.vo.RegisterVo;
import com.learner.pojo.user.vo.SearchRequestVo;
import com.learner.pojo.user.vo.UpdateUserConfigureRequestVo;
import com.learner.pojo.user.vo.UpdateUserInfoRequestVo;
import com.learner.pojo.user.vo.UpdateUserPwdRequestVo;
import com.learner.ucenter.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;


/**
 * @Description: 会员登录注册
 * @Author: ice2020x
 * @Date: 2021/10/24
 */
@Slf4j
@RestController
@RequestMapping("/api/ucenter/member")
public class UserController {

    @Autowired
    private UserService userService;


    /**
     * @Description: 注册
     * @Author: ice2020x
     * @Date: 2021/10/27
     */
    @PostMapping("/register")
    public Map<String, Object> register(@RequestBody RegisterVo registerVo) {
        return userService.register(registerVo);
    }


    /**
     * @Description: 登录
     * @Author: ice2020x
     * @Date: 2021/10/27
     */
    @PostMapping("/login")
    public Map<String, Object> login(@RequestBody LoginVo loginVo) {
        return userService.login(loginVo);
    }

    /**
     * @Description: 通过token获取用户信息
     * @Author: ice2020x
     * @Date: 2021/10/26
     */
    @GetMapping("/getLoginInfo")
    public JwtInfo getLoginInfo(HttpServletRequest request) {
        try {
            return JwtUtils.getInfoByJwtToken(request);
        } catch (Exception e) {
            log.error("未获取到token信息", e);
            throw ExceptionUtil.createException(RestException.class, 1000009);
        }
    }

    /**
     * @Description: 给订单服务提供数据的接口
     * @Author: ice2020x
     * @Date: 2021/10/28
     */
    @GetMapping("/inner/getMemberDto/{memberId}")
    public MemberDto getMemberDtoIdByMemberId(@PathVariable("memberId") String memberId) {
        return userService.getMemberDtoIdByMemberId(memberId);
    }


    /**
     * @Description: 给订单服务提供数据的接口
     * @Author: ice2020x
     * @Date /* 获取验证码图片
     */
    @RequestMapping("/getVerifyCode")
    public void getVerificationCode(HttpServletRequest request, HttpServletResponse response) {
        userService.getVerificationCode(request, response);
    }

    /**
     * 根据用户id获取用户详细信息
     *
     * @param uid     用户id
     * @param request request
     * @return 请求结果
     */
    @GetMapping("/getUserInfo/{uid}")
    public User getUserInfo(@PathVariable("uid") String uid, HttpServletRequest request) {
        return userService.getUserInfo(uid, request);
    }

    /**
     * 根据用户id获取用户详细信息
     *
     * @param userId 用户id
     * @return 请求结果
     */
    @GetMapping("/{userId}")
    public User getUserInfo(@PathVariable("userId") String userId) {
        return userService.getUserInfo(userId);
    }

    /**
     * 添加分组
     *
     * @param requestVo 参数
     */
    @PostMapping("/addFenZu")
    public void addNewFenZu(@RequestBody NewFenZuRequestVo requestVo) {
        userService.addNewFenZu(requestVo);
    }

    /**
     * 修改好友备注
     *
     * @param requestVo 参数
     * @param request   request
     */
    @PostMapping("/updateFriendBeiZhu")
    public void modifyFriendBeiZhu(@RequestBody ModifyFriendBeiZhuRequestVo requestVo, HttpServletRequest request) {
        String currentUserId = JwtUtils.getCurrentUserId(request);
        userService.modifyFriendBeiZhu(requestVo, currentUserId);
    }

    /**
     * 修改好友分组
     *
     * @param requestVo 参数
     * @return 请求结果
     */
    @PostMapping("/modifyFriendFenZu")
    public void modifyFriendFenZu(@RequestBody ModifyFriendFenZuRequestVo requestVo) {
        userService.modifyFriendFenZu(requestVo);
    }

    /**
     * 删除分组
     *
     * @param requestVo 参数
     */
    @PostMapping("/delFenZu")
    public void deleteFenZu(@RequestBody DelFenZuRequestVo requestVo) {
        userService.deleteFenZu(requestVo);
    }

    /**
     * 修改分组名
     *
     * @param requestVo 参数
     */
    @PostMapping("/editFenZuName")
    public void editFenZu(@RequestBody EditFenZuRequestVo requestVo) {
        userService.editFenZu(requestVo);
    }


    /**
     * 修改用户的个性化设置
     *
     * @param requestVo 参数
     * @param request   request
     * @return 请求结果
     */
    @PostMapping("/updateUserConfigure")
    public User updateUserConfigure(@RequestBody UpdateUserConfigureRequestVo requestVo, HttpServletRequest request) {
        String userId = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        boolean res = userService.updateUserConfigure(requestVo, userId);
        if (res) {
            //同时携带用户信息去更新全局的用户信息
            return userService.getUserInfo(userId, request);
        } else {
            return null;
        }
    }


    /**
     * 更新用户信息
     *
     * @param requestVo 参数
     * @param request   request
     */
    @PostMapping("/updateUserInfo")
    public void updateUserInfo(@RequestBody UpdateUserInfoRequestVo requestVo, HttpServletRequest request) {
        String userId = getUserId(request);
        requestVo.setUserId(userId);
        userService.updateUserInfo(requestVo);
    }

    @PostMapping("/updateUserPwd")
    public void updateUserPwd(@RequestBody UpdateUserPwdRequestVo requestVo) {
        userService.updateUserPwd(requestVo);
    }

    @PostMapping("/preFetchUser")
    public HashMap<String, Object> searchUser(@RequestBody SearchRequestVo requestVo, HttpServletRequest request) {
        JwtInfo infoByJwtToken = JwtUtils.getInfoByJwtToken(request);
        String userId = infoByJwtToken.getUserId();
        HashMap<String, Object> userList = userService.searchUser(requestVo, userId);
        System.out.println("搜索的用户信息返回的结果为：" + JSON.toJSONString(userList));
        return userList;
    }

    /**
     * 获取当前登录的用户的uid
     *
     * @return 请求结果
     */
    private String getUserId(HttpServletRequest request) {
        return JwtUtils.getCurrentUserId(request);
    }
}

